/*
	***************************
	* PCMod 2 SS Device Element *
	***************************
	
	 Element: Listbox

*/

local PANEL = {}

function PANEL:Initialize()
	self.List = {}
	self.ScrollBar = false
	self.ScrollOffset = 0
	self.SelItem = ""
end

function PANEL:Paint()
	local c_bor = self:GetCol( "Border" )
	local c_lbbg = self:GetCol( "ListboxBG" )
	local c_lb_txt = self:GetCol( "Listbox_Text" )
	local c_lb_selbg = self:GetCol( "Listbox_SelBG" )
	local c_lb_seltxt = self:GetCol( "Listbox_SelText" )
	if (!self:CallThemeHook( "listbox", self:GetStruct() )) then
		surface.SetDrawColor( c_lbbg.r, c_lbbg.g, c_lbbg.b, c_lbbg.a )
		surface.DrawRect( self.X, self.Y, self.W, self.H )
		surface.DrawOutline( self.X, self.Y, self.W, self.H, c_bor )
	end
	local ypos = 0
	local odec = 0
	surface.SetFont( "pcmod_3d2d" )
	local sbw, sbh = surface.GetTextSize( "XX" )
	local tw = 0
	local th = 0
	for _, v in pairs( self.List ) do
		tw, th = surface.GetTextSize( v )
		break
	end
	local cy = 0
	local cnt = table.Count( self.List )
	local t_h = cnt * th
	if (t_h > self.H) then
		self.ScrollBar = true
	else
		self.ScrollBar = false
		self.ScrollOffset = 0
	end
	local o = self.ScrollOffset
	if (!o) then o = 0 end
	local i = 0
	odec = o/(cnt-(math.floor( self.H / th )))
	for name, text in pairs( self.List ) do
		if (i >= o) then
			local tw, th = surface.GetTextSize( text )
			cy = cy + th
			if (cy < self.H) then
				local tcol = c_lb_txt
				if (self.SelItem == name) then
					surface.SetDrawColor( c_lb_selbg.r, c_lb_selbg.g, c_lb_selbg.b, c_lb_selbg.a )
					surface.DrawRect( self.X+2, self.Y + ypos + 1, self.W-2, th - 1 )
					tcol = c_lb_seltxt
				end
				surface.SetDrawColor( c_bor.r, c_bor.g, c_bor.b, c_bor.a )
				surface.DrawLine( self.X, self.Y + ypos + th, self.X + self.W, self.Y + ypos + th )
				local sa = 0
				if (self.ScrollBar) then sa = sbw * 0.5 end
				draw.SimpleText( text, "pcmod_3d2d", self.MX-sa, self.Y + ypos + (th*0.5), tcol, 1, 1 )
				ypos = ypos + th
			end
		end
		i = i + 1
	end
	if (self.ScrollBar) then
		local tw = sbw
		local th = sbh
		local c_btbg = self:GetCol( "ButtonBG" )
		local c_bt_txt = self:GetCol( "Button_Text" )
		surface.SetDrawColor( c_btbg.r, c_btbg.g, c_btbg.b, c_btbg.a )
		local xpos = self.X+(self.W-tw)
		surface.DrawRect( xpos, self.Y+1, tw, self.H-2 )
		surface.SetDrawColor( c_bor.r, c_bor.g, c_bor.b, c_bor.a )
		surface.DrawLine( xpos, self.Y, xpos, self.Y+self.H )
		surface.DrawLine( xpos, self.Y+th, xpos+tw, self.Y+th )
		surface.DrawLine( xpos, (self.Y+self.H)-th, xpos+tw, (self.Y+self.H)-th )
		if (odec) then
			local oy = self.Y+th+((self.H-(th*2))*odec)
			surface.DrawLine( xpos, oy, xpos+tw, oy )
		end
		draw.SimpleText( "^", "pcmod_3d2d", xpos+(tw/2), self.Y+(th/2), c_bt_txt, 1, 1 )
		draw.SimpleText( "v", "pcmod_3d2d", xpos+(tw/2), (self.Y+self.H)-(th/2), c_bt_txt, 1, 1 )
	end
end

function PANEL:AddItem( name, text )
	self.List[ name ] = text
end

function PANEL:ClearList()
	table.Empty( self.List )
end